package com.ruoyi.equipment.controller;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.equipment.domain.TEquipmentLog;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.equipment.domain.TDrawer;
import com.ruoyi.equipment.service.ITDrawerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 仓匣Controller
 * 
 * @author ruoyi
 * @date 2021-12-20
 */
@RestController
@RequestMapping("/facility/drawer")
public class TDrawerController extends BaseController
{
    @Autowired
    private ITDrawerService tDrawerService;

    /**
     * 查询仓匣列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDrawer tDrawer)
    {
        startPage();
        List<TDrawer> list = tDrawerService.selectTDrawerList(tDrawer);
        return getDataTable(list);
    }

    /**
     * 导出仓匣列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:export')")
    @Log(title = "仓匣", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TDrawer tDrawer)
    {
        List<TDrawer> list = tDrawerService.selectTDrawerList(tDrawer);
        ExcelUtil<TDrawer> util = new ExcelUtil<TDrawer>(TDrawer.class);
        util.exportExcel(response, list, "仓匣数据");
    }

    /**
     * 获取仓匣详细信息
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:query')")
    @GetMapping(value = "/{equipmentId}")
    public AjaxResult getInfo(@PathVariable("equipmentId") String equipmentId)
    {
        return AjaxResult.success(tDrawerService.selectTDrawerByEquipmentId(equipmentId));
    }

    /**
     * 获取仓匣的详细信息 根据编号进行获取
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:query')")
    @GetMapping("/detail/{equipmentId}/{drawerNo}")
    public AjaxResult getDetail(@PathVariable("equipmentId") String equipmentId,@PathVariable("drawerNo") Integer drawerNo){
        TDrawer tDrawer = new TDrawer();
        tDrawer.setDrawerNo(drawerNo);
        tDrawer.setEquipmentId(equipmentId);
        return AjaxResult.success(tDrawerService.selectTDrawerDeatil(tDrawer));
    }

    /**
     * 新增仓匣
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:add')")
    @Log(title = "仓匣", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDrawer tDrawer)
    {
        return toAjax(tDrawerService.insertTDrawer(tDrawer));
    }

    /**
     * 修改仓匣
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:edit')")
    @Log(title = "仓匣", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDrawer tDrawer)
    {
        return toAjax(tDrawerService.updateTDrawer(tDrawer));
    }

    /**
     * 下线仓匣
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:remove')")
    @Log(title = "下线仓匣", businessType = BusinessType.DELETE)
	@DeleteMapping("/{equipmentIds}/{drawerNos}")
    public AjaxResult remove(@PathVariable String[] equipmentIds,@PathVariable Integer[] drawerNos)
    {
        return toAjax(tDrawerService.deleteTDrawerByEquipmentIds(equipmentIds,drawerNos));
    }

    /**
     * 上线仓匣
     */
    @PreAuthorize("@ss.hasPermi('equipment:equipment:remove')")
    @Log(title = "上线仓匣", businessType = BusinessType.DELETE)
    @GetMapping("/popDrawer/{equipmentIds}/{drawerNos}")
    public AjaxResult popDrawer(@PathVariable String[] equipmentIds,@PathVariable Integer[] drawerNos)
    {
        return toAjax(tDrawerService.popTDrawerByEquipmentIds(equipmentIds,drawerNos));
    }
    /**
     * 借出仓匣里的工具
     */

    @PreAuthorize("@ss.hasPermi('equipment:equipment:list')")
    @Log(title = "借出仓匣", businessType = BusinessType.DELETE)
    @PostMapping("/lendDrawer")
    public AjaxResult lendDrawer(@RequestBody TEquipmentLog tEquipmentLog)
    {
        Long userId = getUserId();
        Long deptId = getDeptId();
        tEquipmentLog.setReturnStu(1);
        tEquipmentLog.setUserId(userId.intValue());
        tEquipmentLog.setDeptId(deptId.intValue());
        tEquipmentLog.setUseTime(new Date());
        tEquipmentLog.setDrawerStu(1);
        return toAjax(tDrawerService.lendTDrawer(tEquipmentLog));
    }



}
